<?php
	class MessageModel extends Model{
		function MessageModel(){
	        parent::Model();
	    }
	    /*
		读帖子列表
		$id:版块id
		$offset:分页偏移量
		出参:
			['row']:帖子列表
			['recordcount']:帖子总数
		*/
	    function select($id,$offset=0,$limit=20){
	    	$data['row']=$this->db->select('message.id,message.create_time,user_id,title,top_lv,marrow_lv,nickname')->from('message')->join('user', 'message.user_id = user.id', 'left')->where('parent_id',0)->where('forum_id',$id)->order_by('top_lv','desc')->order_by('message.create_time','desc')->limit($limit,$offset)->get()->result_array();
	    	$data['recordcount']=$this->db->select('count(*) as recordcount')->where('parent_id',0)->where('forum_id',$id)->get('message')->row()->recordcount;
	    	return $data;
	    }
	    /*
	     * 按版块读最后修改一条帖子（首页用）
	     * */
		function get_last_message_by_forum($id){
	    	$data=$this->db->select('message.id,message.create_time,user_id,title,nickname')->from('message')->join('user', 'message.user_id = user.id', 'left')->where('parent_id',0)->where('forum_id',$id)->order_by('message.create_time','desc')->get()->row_array();
	    	if(count($data)==0){
	    		$data=array(
	    			'title'=>'',
	    			'nickname'=>'',
	    			'create_time'=>''
	    		);
	    	}
	    	return $data;
	    }
	    /*
		读单条帖子(以及跟贴)
		$id:帖子id
		$offset:分页偏移量
		出参:
			['row']:跟帖列表
			['recordcount']:帖子总数
		*/
	    function read($id,$offset=0){
	    	$data['row']=$this->db->select('message.*,nickname')->from('message')->join('user', 'message.user_id = user.id', 'left')->where('message.id',$id)->or_where('parent_id',$id)->order_by('message.parent_id','asc')->order_by('message.create_time','desc')->limit(20,$offset)->get()->result_array();
	    	$data['recordcount']=$this->db->select('count(*) as recordcount')->where('message.id',$id)->or_where('parent_id',$id)->get('message')->row()->recordcount;
	    	return $data;
	    }
	    /*
		我的主题帖
		$id:用户id
		$offset:分页偏移量
		出参:
			['row']:帖子列表
			['recordcount']:帖子总数
		*/
	    function my_message($id,$offset=0){
	    	$data['row']=$this->db->select('message.id,message.create_time,user_id,title,top_lv,marrow_lv,nickname')->from('message')->join('user', 'message.user_id = user.id', 'left')->where('user_id',$id)->where('parent_id',0)->order_by('top_lv','desc')->order_by('message.create_time','desc')->limit(20,$offset)->get()->result_array();
		    $data['recordcount']=$this->my_message_num($id);
		  	return $data;
	    }
	    /*
		我的主题帖数
		$id:用户id
		$marrow
			0:所有
			1:精华
		*/
	    function my_message_num($id,$marrow=0){
	    	if($marrow==0){
	    		return $this->db->select('count(*) as recordcount')->where('parent_id',0)->where('user_id',$id)->get('message')->row()->recordcount;
	   		}
	   		else{
	   			return $this->db->select('count(*) as recordcount')->where('parent_id',0)->where('user_id',$id)->where('marrow_lv >',0)->get('message')->row()->recordcount;
	   		}
	    }
	    
	    /*
		我回复的主题帖
		$id:用户id
		$offset:分页偏移量
		出参:
			['row']:帖子列表
			['recordcount']:帖子总数
		
		P.S:不会取出重复的主题帖
		*/
	    function my_reply_message($id,$offset=0){
	    	$sql="select message.id,message.create_time,user_id,title,top_lv,marrow_lv,nickname from message left join user on message.user_id=user.id where parent_id=0 and message.id<>'$id' and message.id in(select parent_id from message where parent_id>0 and user_id='$id')";
		  	$data['row']=$this->db->query($sql)->result_array();
		  	$data['recordcount']=$this->my_reply_message_num($id);
	    	return $data;
	    }
	    /*我回复的主题贴数*/
	    function my_reply_message_num($id){
	    	$sql="select count(*) as recordcount from message where parent_id=0 and message.id<>'$id' and message.id in(select parent_id from message where parent_id>0 and user_id='$id')";
		  	return $this->db->query($sql)->row()->recordcount;
	    }
	    /*我的回复帖数*/
	    function my_reply_num($id){
	    	return $this->db->select('count(*) as recordcount')->where('parent_id >',0)->where('user_id',$id)->get('message')->row()->recordcount;
	    }
	    
	    function insert($data){
	    	$result=$this->db->insert('message',$data);
	    	if($result){
	    		if($data['parent_id']==0){
	    			$this->add_score($data['user_id'],20);
	    		}
	    		else{
	    			$this->add_score($data['user_id'],2);
	    		}
	    	}
	    	return($result);
	    }
	    /*
		加/减精华
		$id:帖子id
		$add:
			1:加
			0:减
		*/
	    function marrow($id,$add=1){
	    	$data['marrow_lv']=$add;
	    	$return=$this->db->where('id',$id)->update('message',$data);
	    	if($result && $add==1){
	    		$user_id=$this->db->select('user_id')->where('id',$id)->get('message')->row()->user_id;
	    		$this->add_score($user_id,50);
	    	}
	    	return $return;
	    }
	    /*
		编辑帖子
		*/
	    function update($data){
	    	$id=$data['id'];
	    	$this->db->where('id',$id)->update('message',$data);
	    }
	    /*
		删帖
		*/
	    function delete($id){
	    	$user_id=$this->db->select('user_id')->where('id',$id)->get('message')->row()->user_id;
	    	$result=$this->db->where('id',$id)->or_where('parent_id',$id)->delete('message');
	    	if($result){
	    		$this->add_score($user_id,-50);
	    	}
	    	return delete;
	    }
	    /*
		加分
		*/
	    function add_score($id,$score){
	    	$this->db->query('update user set score=score+'.$score.' where id='.$id);
	    }
	    /*
	     	指定版块帖子数
	    */
	    function get_message_num_by_forum($id,$today=0){
	    	//$test=$this->db->select('date(create_time),date(now())',false)->get('message')->result_array();
	    	//print_r($test);exit;
	    	if($today){
	    		return ($this->db->select('count(*) as recordcount')->where('parent_id',0)->where('forum_id',$id)->where('date(create_time)','date(now())',false)->get('message')->row()->recordcount);
	    	}
	    	else{
	    		return $this->db->select('count(*) as recordcount')->where('parent_id',0)->where('forum_id',$id)->get('message')->row()->recordcount;
	    	}
	    }
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
?>